package com.wanli.storemobile.homepage;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseActivity;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.PurchaseBean;
import com.wanli.storemobile.event.PurchaseAddEvent;
import com.wanli.storemobile.homepage.adapter.PurchaseListAdapter;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.homepage.model.IHomePageModel;
import com.wanli.storemobile.utils.FastClickUtils;
import com.wanli.storemobile.utils.NumberFormatUtils;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.AppTitleBar;
import com.wanli.storemobile.widget.CommonDialog;
import com.wanli.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddPurchaseActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_new_add)
    TextView tvNewAdd;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.edit_purchaser)
    EditText editPurchaser;
    @BindView(R.id.edit_order_fee)
    EditText editOrderFee;
    private PurchaseListAdapter listAdapter;
    private List<PurchaseBean> beanList;
    private IHomePageModel homePageModel;
    private CommonDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_purchase);
        ButterKnife.bind(this);
        initData();
    }

    public void initData() {
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        beanList.add(new PurchaseBean());
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        listAdapter = new PurchaseListAdapter(beanList);
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            if (FastClickUtils.isFastClick()) {
                return;
            }
            if (type == 1) {
                dialogBuild = new CommonDialog.Builder(mActivity);
                dialogBuild.setTitle("提示");
                dialogBuild.setMessage("是否删除此项?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    beanList.remove(position);
                    listAdapter.notifyDataSetChanged();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();

            }
        });

    }

    @OnClick({R.id.tv_new_add, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_new_add:
                beanList.add(new PurchaseBean());
                listAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_commit:
                if (beanList.size() == 0) {
                    ToastUtil.showShort("请添加项目");
                    return;
                }
                String purchaser = editPurchaser.getText().toString();
                String order_fee = editOrderFee.getText().toString();
                if (TextUtils.isEmpty(purchaser)) {
                    ToastUtil.showShort("请输入采购人姓名");
                    return;
                }
                if (TextUtils.isEmpty(order_fee)) {
                    ToastUtil.showShort("请输入采购单总价");
                    return;
                }

                List<PurchaseBean> dataBeanList = new ArrayList<>();
                for (PurchaseBean dataBean : beanList) {

                    PurchaseBean bean = new PurchaseBean();
                    if (!TextUtils.isEmpty(dataBean.getGoods_fee())) {
                        bean.setGoods_fee(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getGoods_fee()) * 100));
                    }
                    if (!TextUtils.isEmpty(dataBean.getGoods_total_fee())) {
                        bean.setGoods_total_fee(NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getGoods_total_fee()) * 100));
                    }
                    bean.setGoods_name(dataBean.getGoods_name());
                    bean.setGoods_count(dataBean.getGoods_count());

                    dataBeanList.add(bean);
                }
                order_fee = NumberFormatUtils.formatDouble(Double.parseDouble(order_fee) * 100);
                requestStorePurchaseAdd(purchaser, order_fee, dataBeanList);
                break;
        }
    }

    public void requestStorePurchaseAdd(String purchaser, String order_fee, List<PurchaseBean> beanList) {
        ViewLoading.showProgress(mActivity, "保存中......");
        homePageModel.requestStorePurchaseAdd(purchaser, order_fee, beanList, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("保存成功");
                EventBus.getDefault().post(new PurchaseAddEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

}
